#!/bin/bash

set -e

# 重试次数
retry=3

while
    umaf_boot=$(efibootmgr | grep "UMAF" | grep "umaf.efi" | awk '{print $1}' | sed 's/Boot//' | sed 's/\*//')
    [ -z "$umaf_boot" ] && [ $retry -gt 0 ]
do
    if [ $retry -eq 0 ]; then
        echo "UMAF boot entry not found, creating one failed."
        exit 1
    fi
    echo "UMAF boot entry not found, creating one..."
    /usr/libexec/setup-umaf
    retry=$((retry - 1))
done

echo "UMAF boot entry $umaf_boot found, set to next boot..."
efibootmgr -n $umaf_boot
sleep 1
echo "Rebooting..."
reboot
